<?php

namespace app\queue\stomp;

use app\queue\BaseConsumer;
use common\model\PlanModel;
use common\model\TaskModel;
use common\service\FixDataService;
use common\service\OrderService;
use common\service\TaskMemberService;

class FixPlanTodoTaskConsumer extends BaseConsumer
{
    // 要消费的队列名
    public $queue = 'fix_plan_to_do_task';

    // 连接名，对应 config/redis_queue.php 里的连接`
    public $connection = 'default';

    /**
     * 消费
     * @param $data
     */
    public function doConsume($data)
    {
        $planList = $data['plan'];
        $stepCategoryList = $data['step_category'];

        try {
            foreach ($planList as $planItem) {
                $taskResult = TaskMemberService::getInstance()->create([
                    'task' => [
                        "project_id" => $planItem['project_id'],
                        "name" => $stepCategoryList[$planItem['step_category_id']]['name'] ?? "环节工序",
                        "plan_start_time" => date('Y-m-d H:i:s', $planItem['start_time']),
                        "plan_end_time" => date('Y-m-d H:i:s', $planItem['end_time']),
                        "step_type" => TaskModel::STEP_TYPE_TO_DO
                    ],
                    'relate_tag' => [
                        ['tenant_id' => $planItem['tenant_id'], 'link_module_code' => 'episode', 'link_id' => $planItem['entity_id']],
                        ['tenant_id' => $planItem['tenant_id'], 'link_module_code' => 'step_category', 'link_id' => $planItem['step_category_id']]
                    ]
                ], 0, $planItem['tenant_id']);

                model(PlanModel::class)->save([
                    'id' => $planItem['id'],
                    'task_id' => $taskResult['task']['id']
                ]);
            }

        } catch (\Throwable $e) {
            trace(format_exception_info($e), 'ERR');
        }
    }
}
